package com.example.a513;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.viewpager.widget.PagerTabStrip;
import androidx.viewpager.widget.ViewPager;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;

import adpater.BillInfoAdapter;

public class BuildePagerActivity extends AppCompatActivity implements View.OnClickListener, DatePickerDialog.OnDateSetListener {
        private TextView tv_date;
        private ViewPager viewPager;
        private ImageView img_back;
        private TextView tv_bill;
        private PagerTabStrip tabStrip;
        private TextView tv_billist;
        private Calendar calendar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_builde_pager);

        calendar = Calendar.getInstance();
        tv_date = findViewById(R.id.tv_dat);
        viewPager = findViewById(R.id.view_page);
        tv_bill = findViewById(R.id.tv_bill);
        tv_billist = findViewById(R.id.tv_billlist);
        img_back = findViewById(R.id.img_back);
        tv_date.setText(LocalDate.now().toString());
        tv_bill.setText("账单列表");
        tv_billist.setText("添加账单");
        tv_date.setOnClickListener(this);
        tv_billist.setOnClickListener(this);
        img_back.setOnClickListener(this);
        intiViewPager();

    }

    private void intiViewPager() {
        tabStrip = findViewById(R.id.pager_tab);
        tabStrip.setTextSize(TypedValue.COMPLEX_UNIT_SP,17);
        viewPager = findViewById(R.id.view_page);
        BillInfoAdapter adapter = new BillInfoAdapter(getSupportFragmentManager(),calendar.get(Calendar.YEAR));
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(calendar.get(Calendar.MONTH));
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_dat){
            DatePickerDialog dialog = new DatePickerDialog(this, this
                    , calendar.get(Calendar.YEAR)
                    , calendar.get(Calendar.MONTH)
                    , calendar.get(Calendar.DAY_OF_MONTH));
            dialog.show();
        }else if (v.getId() == R.id.tv_billlist){
            Intent intent = new Intent();
            intent.setClass(BuildePagerActivity.this,BillAddActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // 设置栈顶清空
            startActivity(intent);
        }else if ( v.getId() == R.id.img_back){
            finish(); // 结束当前页面
        }
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        calendar.set(Calendar.YEAR,year);
        calendar.set(Calendar.MONTH,month);
        calendar.set(Calendar.DAY_OF_MONTH,dayOfMonth);

        final SimpleDateFormat fmat = new SimpleDateFormat("yyyy-MM");
        Log.e("TAG", "onDateSet: "+ calendar.getTime());
        tv_date.setText(fmat.format(calendar.getTime()));
        viewPager.setCurrentItem(month);
    }
}